Set up

Import des libraries

library(dplyr)
library(readxl)
library(plotly)
library(data.table)

Import des bases de données

client <- fread("CLIENT.csv",sep="|")
entetes <- fread("ENTETES_TICKET_V4.CSV",sep="|",dec=",")

Nous devons ensuite gérer les formats des dates :

client$DATENAISSANCE<- as.Date(client$DATENAISSANCE, format="%d/%m/%Y")
client$DATEDEBUTADHESION<- as.Date(client$DATEDEBUTADHESION, format="%d/%m/%Y")
client$DATEREADHESION<- as.Date(client$DATEREADHESION, format="%d/%m/%Y")
client$DATEFINADHESION<- as.Date(client$DATEFINADHESION, format="%d/%m/%Y")

1 - Etude globale

1.1 Répartition Adhérant / VIP ….

Instruction : Constituer un camembert suivant la répartition suivante :

  • VIP : client étant VIP (VIP = 1)

  • NEW_N2 : client ayant adhéré au cours de l’année N-2 (date début adhésion)

  • NEW_N1 : client ayant adhéré au cours de l’année N-1 (date début adhésion)

  • ADHERANT : client toujours en cours d’adhésion (date de fin d’adhésion > 2018/01/01)

  • CHURNER : client ayant churner (date de fin d’adhésion < 2018/01/01)

Note : le critère le plus au-dessus est prioritaire, exemple : un client étant VIP, et ayant adhéré sur l’année N-1 sera compté comme étant VIP


  • VIP : client étant VIP (VIP = 1)
VIP<-nrow(client[client$VIP==1,])
  • NEW_N2 : client ayant adhéré au cours de l’année N-2 (date début adhésion)
N<-2018
NEW_N2 <-nrow(client[(client$VIP!=1)&(format(client$DATEDEBUTADHESION,"%Y")==(N-2)),])
  • NEW_N1 : client ayant adhéré au cours de l’ann?e N-1 (date début adhésion)
NEW_N1 <-nrow(client[(client$VIP!=1)&(format(client$DATEDEBUTADHESION,"%Y")==(N-1)),])
  • ADHERANT : client toujours en cours d’adhésion (date de fin d’adhésion > 2018/01/01)
ADHERANT <-nrow(client[(client$VIP!=1)&format(client$DATEDEBUTADHESION,"%Y")<(N-2)&
                     (format(client$DATEFINADHESION,"%Y-%m-%d"))>as.Date("2018-01-01"),])
  • CHURNER : client ayant churner (date de fin d’adhésion < 2018/01/01)
CHURNER <-nrow(client[((format(client$DATEFINADHESION,"%Y-%m-%d"))<as.Date("2018-01-01"))&
                        (client$VIP!=1)&format(client$DATEDEBUTADHESION,"%Y")<(N-2),])
  • Visualisation de la répartition
slices <- c(VIP,NEW_N2,NEW_N1,ADHERANT,CHURNER)
levels <- c("# VIP","# adherant au cours de N-2","# adherant au cours de N-1",
          "# Toujours adherant","Churner")

plot_ly(data.frame(cbind(slices,levels)), labels = ~levels,values=~slices, type="pie")%>%
  layout(legend = list(x = -1, y = 0.9))

1.2 Comportement du CA GLOBAL par client N-2 vs N-1

Instruction : Constituer une boite à moustache pour chaque année (N-2 et N-1) comparant le CA TOTAL (TTC) des clients (sommer les achats par client par années)

entetes <- data.table(entetes)
client_CA <- entetes[,list(TOTALCA = sum(TIC_TOTALTTC)),by=list(IDCLIENT,year(TIC_DATE))]

Au vu des premiers résultats lors de l’affichage des boites à moustache, nous devons supprimer les valeurs abérrantes, aussi appelés “outliers”. Pour cela nous supprimons les clients qui ont un TOTALCA supérieur au dernier centile et inférieur au premier centile :

client_CA_not_OUT <- subset(client_CA,(TOTALCA>quantile(client_CA$TOTALCA,c(0.01)))&
                            (TOTALCA<quantile(client_CA$TOTALCA,c(0.99))))

plot_ly(type="box") %>%
  add_boxplot(y=~client_CA_not_OUT[client_CA_not_OUT$year==2016]$TOTALCA,
              boxpoints=FALSE,name="2016") %>%
  add_boxplot(y=~client_CA_not_OUT[client_CA_not_OUT$year==2017]$TOTALCA,
              boxpoints=FALSE,name="2017") %>%
  layout(title="Boite à moustache du CA TOTAL des clients par année d'achat",
         yaxis=list(title="CA Total des clients"))